package com.tangly.codeing.service.impl;

import com.tangly.codeing.dao.ArticleDao;
import com.tangly.codeing.model.Article;
import com.tangly.codeing.service.ArticleService;
import com.tangly.codeing.utils.Constant;
import com.tangly.codeing.utils.IdWoker;
import com.tangly.codeing.utils.PageInfo;
import com.tangly.codeing.utils.ReturnT;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 文章
 * @author tangbing
 * @date 2019-10-30
 */
@Service
public class ArticleServiceImpl implements ArticleService {

	@Resource
	private ArticleDao articleDao;
	@Resource
	private IdWoker idWoker;

	/**
    * 新增
    */
	@Override
	public ReturnT<String> insert(Article article) {

		// valid
		if (article == null) {
			return new ReturnT<>(ReturnT.FAIL_CODE, "必要参数缺失");
        }
		article.setUuid(idWoker.nextStringId());
		article.setUpdateTime(new Date());
		article.setStatus(Constant.DELETE_FLAG);
		articleDao.insert(article);
        return ReturnT.SUCCESS;
	}

	/**
	* 删除
	*/
	@Override
	public ReturnT<String> delete(Long id) {
		int ret = articleDao.delete(id);
		return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	/**
	* 批量删除
	*/
	@Override
	public ReturnT<String> batchDelete(List<String> ids) {
		int ret = articleDao.batchDelete(ids);
		return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
	}

	/**
	* 更新
	*/
	@Override
	public ReturnT<String> update(Article article) {
		int ret = articleDao.update(article);
		return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	/**
	* Load查询
	*/
	@Override
	public Article load(Long id) {
		return articleDao.load(id);
	}

	/**
	* 分页查询
	*/
	@Override
	public PageInfo pageList(PageInfo pageInfo) {

		List<Article> pageList = articleDao.pageList(pageInfo);
		int totalCount = articleDao.pageListCount(pageInfo);

        pageInfo.setResultObject(pageList);
        pageInfo.setTotalRecord(totalCount);
        return pageInfo;
	}

}
